package com.wwl.deliveryappointmentbackend.control.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wwl.deliveryappointmentbackend.model.Deliver;
import com.wwl.deliveryappointmentbackend.service.DeliverService;
import com.wwl.deliveryappointmentbackend.utils.DateUtils;
import com.wwl.deliveryappointmentbackend.utils.FileUtil;
import com.wwl.deliveryappointmentbackend.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/api/deiver")
public class DeliverControl {
    @Value("${Constant.upload.imgPath}")
    private String imgPath;
    @Autowired
    private MultipartProperties multipartProperties;
    @Autowired
    private DeliverService deliverService;

    @PostMapping("/saveDeliver")
    public Response saveDeliver(@RequestParam("date") String date,
                                       @RequestParam("time") String time,
                                @RequestParam("qingdanimg") MultipartFile[] qingdanimgs,
                                       @RequestParam("logsticimg") MultipartFile[] logsticimgs) {


        String  qingdanimgStr = uploadPic(qingdanimgs);
        String logsticimgStr =  uploadPic(logsticimgs);
        Deliver deliver = new Deliver();
        deliver.setDeliverDate(date);
        deliver.setDeliverTime(time);
        deliver.setLogstic(qingdanimgStr);
        deliver.setQingdan(logsticimgStr);
        deliverService.saveDeliver(deliver);
        return Response.yes();
    }

    public String uploadPic(MultipartFile[] imgs){
        StringBuilder picList=new StringBuilder();
        if(imgs!=null && imgs.length>0){

            Path currentDir = Paths.get(imgPath);
            Path filePath = currentDir.toAbsolutePath();
            FileUtil.createDirectoryIfNotExists(filePath.toString());
            for (int i=0;i<imgs.length;i++) {
                MultipartFile image = imgs[0];
                String fileName = image.getOriginalFilename();
                fileName  = DateUtils.formatDateTime(new Date(),"yyyMMddHHmmss")+"_"+fileName;
                try {
                    String tempFilePath =filePath+File.separator+ fileName; // 指定保存文件的路径
                    image.transferTo(new File(tempFilePath));
                    picList.append(fileName);
                    if(i != imgs.length-1){
                        picList.append(";");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return picList.toString();
    }

    @GetMapping("/users")
    public Response<IPage<Deliver>> getUsers(@RequestParam(defaultValue = "1") int pageNum,
                                             @RequestParam String Openid,
                                   @RequestParam(defaultValue = "10") int pageSize) {
        return  Response.yes(deliverService.findByOpenid(Openid,pageNum, pageSize));
    }
}
